package com.itheima.web;

import com.itheima.daomain.User;
import com.itheima.service.updateUserServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @Descriptionn:test
 * @Create by:依然
 * @Date:2021/11/3 10:57
 */
@WebServlet("/updateUserByIDServlet")
public class UpdateUserByIDServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        updateUserServlet userServlet = new updateUserServlet();
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        Integer updateuser = userServlet.updateuser(user);
        if (updateuser > 0) {
            response.sendRedirect("/update_ok.jsp");
        } else {
            System.out.println("修改失败");
        }

    }
}
